package cn.openea.perpartner.meily.web.service.importandexport;

import cn.openea.perpartner.common.core.exception.ServiceException;
import cn.openea.perpartner.common.core.utils.poi.ExcelUtil;
import cn.openea.perpartner.meily.constant.CommonConstants;
import cn.openea.perpartner.meily.web.entity.GroupBuyingSub;
import cn.openea.perpartner.meily.web.entity.dto.GroupBuyingSubDTOFactory;
import cn.openea.perpartner.meily.web.entity.dto.GroupBuyingSubDTOFactory.SupplyPriceTemplateDTO;
import cn.openea.perpartner.meily.web.entity.dto.ImportErrMsgDTO;
import cn.openea.perpartner.meily.web.entity.vo.ExportDataVo;
import cn.openea.perpartner.meily.web.entity.vo.GroupBuyingSubEditVo;
import cn.openea.perpartner.meily.web.entity.vo.OrderRecordVo;
import cn.openea.perpartner.meily.web.service.IGroupBuyingSubService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author ysk
 * @version 1.0.1
 * @Description 团购数据录入供货价
 * @createTime 2023年05月16日 22:57:00
 */
@Slf4j
@Service(value = CommonConstants.IMPORTANDEXPORT.GROUPBUY_ENTER_SUPPLYPRICE)
public class GroupbuyEnterSupplypriceService extends ImportAndExportAbstractService {

    @Resource
    private IGroupBuyingSubService groupBuyingSubService;
    @Resource
    private GroupBuyingSubDTOFactory groupBuyingSubDTOFactory;

    @Override
    public void exportExcel(ExportDataVo exportDataVo, HttpServletResponse response) {
        // 发货模板默认查询的是待发货的数据
        List<GroupBuyingSub> groupBuyingSubs;
        if (exportDataVo.byIds()) {
            groupBuyingSubs = groupBuyingSubService.selectByIdsAndOrderState(exportDataVo.getBatchIds(), "已付款");
        } else {
            OrderRecordVo recordVo = JSONObject.parseObject(JSONObject.toJSONString(exportDataVo.getQueryVO()), OrderRecordVo.class);
            if (recordVo == null) {
                recordVo = new OrderRecordVo();
            }
            groupBuyingSubs = groupBuyingSubService.selectList(recordVo);
        }
        // 导入供货价
        List<SupplyPriceTemplateDTO> supplyPriceTemplateDTOS = groupBuyingSubs
                .parallelStream()
                .map(item -> groupBuyingSubDTOFactory.toSupplyPriceTemplateDTO.apply(item))
                .collect(Collectors.toList());
        ExcelUtil<SupplyPriceTemplateDTO> util = new ExcelUtil<>(SupplyPriceTemplateDTO.class);
        util.exportExcel(response, supplyPriceTemplateDTOS, "订单录入供货价模板.xlsx");
    }

    @Override
    protected ImportErrMsgDTO handleSingleExcel(Object obj) {
        SupplyPriceTemplateDTO supplyPriceTemplateDTO = (SupplyPriceTemplateDTO) obj;
        try {
            if (StringUtils.isBlank(supplyPriceTemplateDTO.getOrderId())) {
                throw new ServiceException("订单编号为空");
            }
            if (supplyPriceTemplateDTO.getRealSupplyPrice() == null) {
                throw new ServiceException("实际供货价为空");
            }
            GroupBuyingSub groupBuyingSub = Optional.ofNullable(groupBuyingSubService.findByOrderId(supplyPriceTemplateDTO.getOrderId()))
                    .orElseThrow(() -> new ServiceException("订单不存在"));
            groupBuyingSubService.updateGroupBuyOrder(GroupBuyingSubEditVo.builder()
                    .id(groupBuyingSub.getId())
                    .realSupplyPrice(supplyPriceTemplateDTO.getRealSupplyPrice())
                    .build());
        } catch (Exception e) {
            return ImportErrMsgDTO.builder()
                    .id(supplyPriceTemplateDTO.getOrderId())
                    .errMsg(e.getMessage())
                    .build();
        }
        return null;
    }
}
